package me.goldze.mvvmhabit.http;
import com.google.gson.Gson;

import org.json.JSONException;

import io.reactivex.observers.DisposableObserver;
import me.goldze.mvvmhabit.utils.Utils;

/**
 * 统一的Code封装处理
 */
public abstract class ApiDisposableObserver<T> extends DisposableObserver<T> {
    public abstract void onResult(Object t);
    @Override
    public void onComplete() {
    }

    @Override
    public void onError(Throwable e) {
        e.printStackTrace();
    }

    @Override
    public void onStart() {
        super.onStart();
        if (!NetworkUtil.isNetworkAvailable(Utils.getContext())) {
            onComplete();
        }
    }

    @Override
    public void onNext(Object o) {
        try {
            onResult(o);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public final static boolean isJSONValid(String test) {
        try {
            new Gson().fromJson(test,String.class);
        } catch (Exception ex) {
            return false;
        }
        return true;
    }

    public static final class CodeRule {
        //业务成功, 正确的操作方式
        static final int CODE_200 = 200;
    }
}